package com.tencent.polaris.discovery.example.provider;

import com.tencent.polaris.api.core.ProviderAPI;
import com.tencent.polaris.api.rpc.InstanceRegisterRequest;
import com.tencent.polaris.api.rpc.InstanceRegisterResponse;
import com.tencent.polaris.discovery.example.utils.ExampleUtils;
import com.tencent.polaris.discovery.example.utils.ExampleUtils.InitResult;

public class RegisterExample {

    public static void main(String[] args) throws Exception {
        InitResult initResult = ExampleUtils.initProviderConfiguration(args);
        String namespace = initResult.getNamespace();
        String service = initResult.getService();
        String host = initResult.getHost();
        int port = initResult.getPort();
        String token = initResult.getToken();
        try (ProviderAPI providerAPI = ExampleUtils.createProviderAPI(initResult.getConfig())) {
            InstanceRegisterRequest instanceRegisterRequest = new InstanceRegisterRequest();
            instanceRegisterRequest.setNamespace(namespace);
            instanceRegisterRequest.setService(service);
            instanceRegisterRequest.setHost(host);
            instanceRegisterRequest.setPort(port);
            instanceRegisterRequest.setToken(token);
            if (initResult.getTtl() > 0) {
                instanceRegisterRequest.setTtl(initResult.getTtl());
            }
            InstanceRegisterResponse instanceRegisterResponse = providerAPI.register(instanceRegisterRequest);
            System.out.println("response after register is " + instanceRegisterResponse);
        }
    }
}